Conditions | 1 |
Paths | 1 |
Total Lines | 148 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import chai from 'chai'; |
||
15 | describe(`[module] configure`, function() { |
||
16 | describe(`[method] load`, function() { |
||
17 | let defaultconfpath = `.codingamerc`; |
||
18 | let incorrectconfpath = `.codingamerc.txt`; |
||
19 | let defaultconf = { |
||
20 | "username": `me`, |
||
21 | "password": `somepass`, |
||
22 | "exercise": `5711567e959cf54dd2dd79c1b4c259560d6ba46`, |
||
23 | "tests": [1, 2], |
||
24 | "language": `Python`, |
||
25 | "bundle": `bundle.py` |
||
26 | }; |
||
27 | before(function() { |
||
28 | mockfs({ |
||
29 | [defaultconfpath]: JSON.stringify(defaultconf), |
||
30 | [incorrectconfpath]: `This is not valid JSON!` |
||
31 | }); |
||
32 | }); |
||
33 | after(function() { |
||
34 | mockfs.restore(); |
||
35 | }); |
||
36 | afterEach(function() { |
||
37 | for (let param in defaultconf) { |
||
38 | configure.forget(param); |
||
39 | } |
||
40 | }); |
||
41 | |||
42 | it(`should return the content of file parsed as JSON`, function() { |
||
43 | let conf = configure.load(defaultconfpath); |
||
44 | return expect(conf).to.eventually.be.deep.equal(defaultconf); |
||
45 | }); |
||
46 | |||
47 | it(`should reject because of incorrect file path`, function() { |
||
48 | let conf = configure.load(`/incorrect/path/.codingamerc`); |
||
49 | return expect(conf).to.eventually.be.rejected; |
||
50 | }); |
||
51 | |||
52 | it(`should reject because of incorrect parsing of JSON`, function() { |
||
53 | let conf = configure.load(incorrectconfpath); |
||
54 | return expect(conf).to.eventually.be.rejected; |
||
55 | }) |
||
56 | |||
57 | it(`should return options if path is incorrect and options is defined`, function() { |
||
58 | let conf = configure.load(undefined, defaultconf); |
||
59 | return expect(conf).to.eventually.be.deep.equal(defaultconf); |
||
60 | }); |
||
61 | }); |
||
62 | describe(`[method] get`, function() { |
||
63 | let filepath = `file.txt`; |
||
64 | let filecontent = `Hello world!` |
||
65 | let shellcmd = [`echo`, filecontent]; |
||
66 | let defaultconf = { |
||
67 | "shell": shellcmd, |
||
68 | "wrongshell": [`notacommand`], |
||
69 | "path": filepath |
||
70 | }; |
||
71 | let answer = `42`; |
||
72 | let createInterface; |
||
73 | before(function(done) { |
||
74 | mockfs({ |
||
75 | [filepath]: filecontent |
||
76 | }); |
||
77 | configure.load(undefined, defaultconf).then(function() { |
||
78 | done(); |
||
79 | }) |
||
80 | }); |
||
81 | after(function() { |
||
82 | mockfs.restore(); |
||
83 | for (let param in defaultconf) { |
||
84 | configure.forget(param); |
||
85 | } |
||
86 | }); |
||
87 | beforeEach(function() { |
||
88 | createInterface = sinon.stub(readline, 'createInterface', function() { |
||
89 | return { |
||
90 | "question": function(question, cb) { |
||
91 | cb(answer); |
||
92 | }, |
||
93 | "close": function() {} |
||
94 | }; |
||
95 | }); |
||
96 | }); |
||
97 | afterEach(function() { |
||
98 | createInterface.restore(); |
||
99 | }); |
||
100 | it(`should return the string property as it is`, function() { |
||
101 | let get = configure.get(`path`); |
||
102 | return expect(get).to.eventually.be.equal(filepath); |
||
103 | }); |
||
104 | it(`should return the array property as it is`, function() { |
||
105 | let get = configure.get(`shell`); |
||
106 | return expect(get).to.eventually.be.deep.equal(shellcmd); |
||
107 | }); |
||
108 | it(`should return the result of the shell command`, function() { |
||
109 | let get = configure.get(`shell`, `shell`); |
||
110 | return expect(get).to.eventually.be.deep.equal(filecontent); |
||
111 | }); |
||
112 | it(`should return the content of the file`, function() { |
||
113 | let get = configure.get(`path`, `file`); |
||
114 | return expect(get).to.eventually.be.deep.equal({ |
||
115 | "path": filepath, |
||
116 | "data": filecontent |
||
117 | }); |
||
118 | }); |
||
119 | it(`should return answer to the question`, function() { |
||
120 | let ask = `ask something?`; |
||
121 | let get = configure.get(`questionproperty`, ``, ask); |
||
122 | return expect(get).to.eventually.be.equal(answer); |
||
123 | }); |
||
124 | it(`should reject because property doesn't exist`, function() { |
||
125 | let get = configure.get(`notaproperty`); |
||
126 | return expect(get).to.eventually.be.rejected; |
||
127 | }); |
||
128 | it(`should reject if first parameter is not a string`, function() { |
||
129 | let get = configure.get(null); |
||
130 | return expect(get).to.eventually.be.rejected; |
||
131 | }); |
||
132 | it(`should reject if shell command is failing`, function() { |
||
133 | let get = configure.get(`wrongshell`, `shell`); |
||
134 | return expect(get).to.eventually.be.rejected; |
||
135 | }); |
||
136 | it(`should reject if question is not a string`, function() { |
||
137 | let get = configure.get(`incorrectquestionproperty`, ``, 42); |
||
138 | return expect(get).to.eventually.be.rejected; |
||
139 | }); |
||
140 | }); |
||
141 | describe(`[method] forget`, function() { |
||
142 | let conf = { |
||
143 | "property": `value` |
||
144 | }; |
||
145 | before(function() { |
||
146 | configure.load(undefined, conf); |
||
147 | }) |
||
148 | it(`should forget about the parameter`, function() { |
||
149 | configure.forget(`property`); |
||
150 | let get = configure.get(`property`); |
||
151 | return expect(get).to.eventually.be.rejected; |
||
152 | }); |
||
153 | it(`should throw an error when the property is not a string`, function() { |
||
154 | try { |
||
155 | configure.forget(null); |
||
156 | } catch(error) { |
||
157 | return expect(error).to.be.an('error'); |
||
158 | } |
||
159 | return expect(false).to.be.ok; |
||
160 | }); |
||
161 | }); |
||
162 | }); |
||
163 |